package com.ruoyi.system.wnjk.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.wnjk.contents.Contents;
import com.ruoyi.system.wnjk.domain.dto.ReceiveItemDTO;
import com.ruoyi.system.wnjk.domain.po.ShopAfterSalesRequests;
import com.ruoyi.system.wnjk.domain.po.ShopOrder;
import com.ruoyi.system.wnjk.mapper.ShopAfterSalesRequestsMapper;
import com.ruoyi.system.wnjk.mapper.ShopOrderMapper;
import com.ruoyi.system.wnjk.service.IShopAfterSalesRequestsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 售后详情Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-07
 */
@Service
public class ShopAfterSalesRequestsServiceImpl implements IShopAfterSalesRequestsService
{
    @Autowired
    private ShopAfterSalesRequestsMapper shopAfterSalesRequestsMapper;

    @Autowired
    private ShopOrderMapper shopOrderMapper;

    /**
     * 查询售后详情
     *
     * @param id 售后详情主键
     * @return 售后详情
     */
    @Override
    public ShopAfterSalesRequests selectShopAfterSalesRequestsById(Long id)
    {
        return shopAfterSalesRequestsMapper.selectShopAfterSalesRequestsById(id);
    }

    /**
     * 查询售后详情列表
     *
     * @param shopAfterSalesRequests 售后详情
     * @return 售后详情
     */
    @Override
    public List<ShopAfterSalesRequests> selectShopAfterSalesRequestsList(ShopAfterSalesRequests shopAfterSalesRequests)
    {
        return shopAfterSalesRequestsMapper.selectShopAfterSalesRequestsList(shopAfterSalesRequests);
    }

    /**
     * 新增售后详情
     *
     * @param shopAfterSalesRequests 售后详情
     * @return 结果
     */
    @Override
    public int insertShopAfterSalesRequests(ShopAfterSalesRequests shopAfterSalesRequests)
    {
        shopAfterSalesRequests.setCreateTime(DateUtils.getNowDate());
        return shopAfterSalesRequestsMapper.insertShopAfterSalesRequests(shopAfterSalesRequests);
    }

    /**
     * 修改售后详情
     *
     * @param shopAfterSalesRequests 售后详情
     * @return 结果
     */
    @Override
    public int updateShopAfterSalesRequests(ShopAfterSalesRequests shopAfterSalesRequests)
    {
        shopAfterSalesRequests.setUpdateTime(DateUtils.getNowDate());
        return shopAfterSalesRequestsMapper.updateShopAfterSalesRequests(shopAfterSalesRequests);
    }

    /**
     * 批量删除售后详情
     *
     * @param ids 需要删除的售后详情主键
     * @return 结果
     */
    @Override
    public int deleteShopAfterSalesRequestsByIds(Long[] ids)
    {
        return shopAfterSalesRequestsMapper.deleteShopAfterSalesRequestsByIds(ids);
    }

    /**
     * 删除售后详情信息
     *
     * @param id 售后详情主键
     * @return 结果
     */
    @Override
    public int deleteShopAfterSalesRequestsById(Long id)
    {
        return shopAfterSalesRequestsMapper.deleteShopAfterSalesRequestsById(id);
    }

    @Transactional
    @Override
    public int postAfterSaleRequest(ShopAfterSalesRequests shopAfterSalesRequests) {
        ShopOrder shopOrder = shopOrderMapper.selectShopOrderById(shopAfterSalesRequests.getOrderId());
        if (shopAfterSalesRequests.getRequestType().equals(Contents.Shop.AFTER_SALES_REQUESTS_STATUS_REFUND)){
            shopOrder.setStatus(Contents.Shop.ORDER_REFUND);
        }else if (shopAfterSalesRequests.getRequestType().equals(Contents.Shop.AFTER_SALES_REQUESTS_STATUS_EXCHANGE)){
            shopOrder.setStatus(Contents.Shop.ORDER_EXCHANGE);
        }
        shopOrderMapper.updateShopOrder(shopOrder);
        shopAfterSalesRequests.setCreateTime(DateUtils.getNowDate());
        return shopAfterSalesRequestsMapper.insertShopAfterSalesRequests(shopAfterSalesRequests);
    }

    @Override
    public boolean refuseAfterSalesRequest(Long id) {
        ShopAfterSalesRequests shopAfterSalesRequests = shopAfterSalesRequestsMapper.selectShopAfterSalesRequestsById(id);
        shopAfterSalesRequests.setRequestStatus(Contents.Shop.REQUESTS_STATUS_REJECTED);
        int row = shopAfterSalesRequestsMapper.updateShopAfterSalesRequests(shopAfterSalesRequests);
        return row>0;
    }

    @Override
    public int receiveItem(ReceiveItemDTO receiveItemDTO) {
        ShopOrder shopOrder = shopOrderMapper.selectShopOrderById(Long.parseLong(receiveItemDTO.getOrderId()));
        shopOrder.setStatus(4);
        int row = shopOrderMapper.updateShopOrder(shopOrder);
        return row;
    }
}
